package com.imooc.sell.controller;

import com.imooc.sell.Enums.ResultEnum;
import com.imooc.sell.converter.OrserFrom2OrderVo;
import com.imooc.sell.dto.OrderDto;
import com.imooc.sell.exception.SellException;
import com.imooc.sell.from.OrderFrom;
import com.imooc.sell.service.BuyerService;
import com.imooc.sell.service.OrderService;
import com.imooc.sell.util.ResultVoUtil;
import com.imooc.sell.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 小柯
 * @Date 2019/7/7  12:01
 **/
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;


    //创建订单
    @RequestMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderFrom orderFrom, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确,orderFrom={}", orderFrom);
            throw new SellException(ResultEnum.PRODUCT_PARAMETER_ERROR.getCode(), bindingResult
                    .getFieldError().getDefaultMessage());
        }

        OrderDto orderDto = OrserFrom2OrderVo.convert(orderFrom);
        if(CollectionUtils.isEmpty(orderDto.getOrderDetailList())){
            log.error("【创建订单】购物车转化为空，转化失败");
            throw new SellException(ResultEnum.PRODTCT_CART_NOT_EXIST);
        }

        OrderDto orderDtoResult = orderService.create(orderDto);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderDtoResult.getOrderId());
        return ResultVoUtil.success(map);
    }

    //订单列表
    @RequestMapping("/list")
    public ResultVo<List<OrderDto>> list(@RequestParam(value = "openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){

        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PRODUCT_PARAMETER_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page, size);

        Page<OrderDto> orderDtoPage = orderService.findList(openid, pageRequest);


        return ResultVoUtil.success(orderDtoPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){

        OrderDto orderOne = buyerService.findOrderOne(openid, orderId);
        return ResultVoUtil.success(orderOne);

    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){

        OrderDto orderDto = buyerService.cancelOrder(openid, orderId);

        return ResultVoUtil.success();
    }


}
